<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/mytag.jspf"%>
<!DOCTYPE html>
<html>
<head>
<base href="<%=basePath%>">
<title>结账</title>
<script type="text/javascript">
$("#pay-btn").on("click",function(){
	var $btn = $(this);
	var payAmount = $("#payAmount").val();
	var orderInfoId = $("#orderInfoId").val();
	var vipId = $("#vipId").val();
	var payWay = $("input[name='payWay']:checked").val();
	var para = "orderInfoId="+orderInfoId;
	var $coupon = $("#coupon-sele");
	var oldText = $btn.text();
	if($coupon.length>0){
		para = para + "&serviceItemId="+$coupon.val();
	}
	if(payWay=="YE"){
		var result = false;
		$btn.attr("disabled","disabled").text("正在提交...");
		$.ajax({
			type : 'POST',
            url: "${ctx}/weixin/employee/orderPayByYE",
            dataType: "json",
            data:para,
            success: function (data) {
                if (data.success) {
                	var msg = "您用余额支付，价格要调成非会员价，非会员价的订单金额为"+data.params.amount+"元，是否确定？";
                	var result = confirm(msg);
            		if(!result){
            			$btn.prop("disabled",false).text(oldText);
            			return;
            		}
            		para = para + "&payWay="+payWay;
            		$btn.attr("disabled","disabled").text("正在提交...");
            		$.ajax({
            			type : 'POST',
                        url: "${ctx}/weixin/employee/orderPaySave",
                        dataType: "json",
                        data:para,
                        success: function (data) {
                        	$btn.prop("disabled",false).text(oldText);
                            if (data.success) {
                            	alert("支付成功");
                            	window.location.href="${ctx}/weixin/employee/vipInfo?vipId="+vipId+"&orderInfoId="+orderInfoId;
                            }else{
                            	Comm.msg(data.msg);
                            }
                        },
                        error: function () {
                        	Comm.msg("网络连接失败，请稍后重试");
                        	$btn.prop("disabled",false).text(oldText);
                        }
                    });
                }else{
                	$btn.prop("disabled",false).text(oldText);
                	Comm.msg(data.msg);
                }
            },
            error: function () {
            	Comm.msg("网络连接失败，请稍后重试");
            	$btn.prop("disabled",false).text(oldText);
            }
        });
	}else if(payWay=="ZX"){
		if(payAmount=="0"){
			Comm.msg("需支付的金额为0元，请选择余额或者现金支付");
		}else{
			window.location.href="${ctx}/weixin/employee/orderPayQrcode?"+para;
		}
	}else{
		para = para + "&payWay="+payWay;
		$btn.attr("disabled","disabled").text("正在提交...");
		$.ajax({
			type : 'POST',
            url: "${ctx}/weixin/employee/orderPaySave",
            dataType: "json",
            data:para,
            success: function (data) {
            	$btn.prop("disabled",false).text(oldText);
                if (data.success) {
                	alert("支付成功");
                	window.location.href="${ctx}/weixin/employee/vipInfo?vipId="+vipId+"&orderInfoId="+orderInfoId;
                }else{
                	Comm.msg(data.msg);
                }
            },
            error: function () {
            	Comm.msg("网络连接失败，请稍后重试");
            	$btn.prop("disabled",false).text(oldText);
            }
        });
	}
});

$("#coupon-sele").on("change",function(){
	var serviceItemId = $(this).val();
	var amount = $("#amount").val();
	if(serviceItemId!=""){
		var price=$("li[data-id='"+serviceItemId+"']").data("price");
		var total = parseFloat(amount)-parseFloat(price);
		$("#total").text(amount+"-"+price+"="+total);
		$("#payAmount").val(total);
	}else{
		$("#total").text(amount);
	}
});
//修改浏览器历史的url地址
if(window.location.href.indexOf('#forward')<0)
	window.history.replaceState('forward', null, window.location.href+'#forward');
else
	window.location.href="${ctx}/weixin/employee/orderPay/${orderInfo.orderInfoId}";
</script>
</head>
<body>
<c:if test="${not empty resultMsg}">
    <script type="text/javascript">
    if(window.location.href.indexOf('#forward')<0)//判断是否是回退
    	Comm.msg("${resultMsg.msg}");
	</script>
</c:if>
	<div style="padding-bottom:150px;" class="am-cf">
		<div class="am-u-sm-12 am-padding-vertical-sm">
			<div class="am-panel am-panel-landiao">
				<div class="am-panel-hd am-text-bold">订单编号：${orderInfo.orderNo}</div>
				<div class="am-panel-bd am-padding-0">
					<ul class="am-list am-list-static am-margin-0">
						<c:forEach items="${details}" var="d" varStatus="status">
							<c:choose>
								<c:when test="${not empty d.mdseNum}"><li>&nbsp;&nbsp;${status.count}.${d.serviceItemName}x${d.mdseNum}—— ${d.price}元</li></c:when>
								<c:when test="${not empty d.serviceItemId}"><li data-id="${d.serviceItemId}" data-price="${d.price}">&nbsp;&nbsp;${status.count}.${d.serviceItemName}（${d.employeeNames}）—— ${d.price}元</li></c:when>
								<c:otherwise><li>&nbsp;&nbsp;${status.count}.${d.serviceItemName}（${d.employeeNames}）—— ${d.price}元</li></c:otherwise>
							</c:choose>
						</c:forEach>
						<li class="am-text-right">合计：<span id="total">${total}</span>元 &nbsp;&nbsp;</li>
					</ul>
				</div>
			</div>
		</div>
		<c:if test="${not empty coupons}">
		<div class="am-g am-bg-grey">
			<div class="am-u-sm-12 am-u-end am-padding-sm">
				&nbsp;&nbsp;选择代金券：<select id="coupon-sele" data-am-selected="{btnWidth: '50%',btnStyle: 'secondary'}">
					<option value="">请选择代金券</option>
					<c:forEach items="${coupons}" var="c">
						<option value="${c.serviceItemId}">${c.serviceName}</option>
					</c:forEach>
				</select>
			</div>
		</div>
		</c:if>
	</div>
	
	<div class="app-fix-bottom">
		<div class="am-text-center am-text-blue am-bg-white am-padding-bottom-xs">支付方式</div>
		<div class="am-g am-padding-bottom-sm am-bg-grey am-padding-top" style="position:relative">
			<img src="assets/img/hr.png" style="position:absolute;top:0;width:100%">
			<input id="orderInfoId" name="orderInfoId" type="hidden" value="${orderInfo.orderInfoId}"></input>
			<input id="amount" name="amount" type="hidden" value="${orderInfo.amount}"></input>
			<input id="payAmount" type="hidden" value="${orderInfo.amount}"></input>
			<input id="vipId" name="vipId" type="hidden" value="${orderInfo.vipId}"></input>
			<c:if test="${not empty orderInfo.vipId}">
			<div class="am-u-sm-12">
				<label class="am-radio-inline"><input type="radio" name="payWay" value="YE" data-am-ucheck> 余额支付</label>
			</div>
			</c:if>
			<div class="am-u-sm-12">
				<label class="am-radio-inline"><input type="radio" name="payWay" value="XJ" data-am-ucheck> 现金支付</label>
			</div>
			<div class="am-u-sm-12">
				<label class="am-radio-inline"><input type="radio" name="payWay" value="ZX" data-am-ucheck checked="checked"> 在线支付</label>
			</div>
		</div>
		<button id="pay-btn" class="am-btn am-btn-lg am-btn-landiao am-btn-block">支付</button>
	</div>
</body>
</html>
